package com.exam.controller;

import com.exam.entity.Student;
import com.exam.service.AdminService;
import com.exam.service.StudentService;
import com.exam.util.Encrypt;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Api(value="admin")
@RestController
public class AdminController {

    @Resource
    private AdminService adminService;

    @Autowired
    private StudentService studentService;


    @Autowired
    private JavaMailSender mailSender;

    @RequestMapping("admin")
    @ApiOperation(value="根据ID获取用户信息",httpMethod="GET",notes="get user by id")
    public Object getAllAdmins(String op){
        if(op!=null && !"".equals(op)){
            if("all".equals(op)){
                List<Map<String, Object>> list = adminService.getAllAdmins();
                return list;
            }
        }
        return null;
    }

    @RequestMapping("email")
    public String sendEmail(String title,String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("723491813@qq.com");
        message.setTo("1153693791@qq.com");
        message.setSubject("主题："+title);
        message.setText(content);
        return send(message);
    }

    //找回密码(通过学号，邮箱)
    @RequestMapping("findPassword")
    public String findPassword(String sno, String email, HttpServletRequest request,HttpSession session){
        Student student = studentService.getBySno(sno);
        if(student == null)//检验用户是否存在
            return "Student is null";
        if(!email.equals(student.getSemail())){//检验邮箱是否正确
            return "Email is Error";
        }
        SimpleMailMessage message = new SimpleMailMessage();//开始编写邮件内容
        message.setFrom("723491813@qq.com");
        message.setTo(student.getSemail());
        message.setSubject("主题：找回密码");
        String sign = Encrypt.md5AndSha(UUID.randomUUID().toString());//加密的标识符
        String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();//服务器地址
        String resetPath = basePath+"/reset.action?sign="+sign+"&sno="+sno;//完整重置路径
        message.setText("请勿回复本邮件.点击下面的链接,重置密码,本邮件超过30分钟,链接将会失效，需要重新申请找回密码."+resetPath);
        Date outDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);// 30分钟后过期
        //将过期时间和标识符保存在session中
        session.setAttribute("resetDate",outDate.getTime());
        session.setAttribute("resetSign",sign);
        System.out.println(session.getAttribute("resetDate"));
        return send(message);
    }

    private String send(SimpleMailMessage message){
        System.out.println("Send loading...");
        try{
            mailSender.send(message);
        }catch (Exception e){
            return "Send Error！";
        }
        return "Success！";
    }
}
